﻿using AutoMapper;
using CSRedis;
using SH.Common;
using SH.Common.CommonReturn;
using SH.Common.DataDictionary;
using SH.Customer.Application.IApplication.IMenberCard;
using SH.Customer.Domain.CustomerClass;
using SH.Customer.Infrastructure.IRepository;
using SH.Customer.Infrastructure.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Dm.net.buffer.ByteArrayBuffer;

namespace SH.Customer.Application.Application
{
    public class MemberCardApplication : BaseApplication<MemberCard>, IMemberCardApplication
    {
        private readonly IMemberCardRepository _memberCardRepository;

        private readonly IMapper _mapper;

        public MemberCardApplication(IMemberCardRepository memberCardRepository, IMapper mapper) : base(memberCardRepository)
        {
            _memberCardRepository = memberCardRepository;
            _mapper = mapper;
        }

        /// <summary>
        /// 添加会员卡
        /// </summary>
        /// <param name="cardDto"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> MemberCardAdd(MemberCardDto cardDto)
        {
            ApiResult<int> result = new ApiResult<int>();

            var dto = _mapper.Map<MemberCard>(cardDto);

            //验证会员卡是否存在
            var card = await _memberCardRepository.GetModelByConditionAsync(m => m.VIPCard == dto.VIPCard);
            if (card != null) 
            {
                result.Error(ApiResult<int>.ErrorType.Request, "已有相同的会员卡号");
                return result;
            }
            var info = await _memberCardRepository.AddRetunIdentityAsync(dto);

            result.Success(info);

            return result;
        }

        /// <summary>
        /// 了解会员卡详情
        /// </summary>
        /// <param name="mId"></param>
        /// <returns></returns>
        public async Task<MemberCard> GetMemberCardDelitalAsync(int mId)
        {
            var list = await _memberCardRepository.GetModelByConditionAsync(m => m.MId == mId);
            return list;
        }

        /// <summary>
        /// 编辑会员信息
        /// </summary>
        /// <param name="cardDto"></param>
        /// <returns></returns>
        public ApiResult<int> MemberCardUpdate(MemberCardDto cardDto)
        {
            ApiResult<int> result = new ApiResult<int>();

            var item = _mapper.Map<MemberCard>(cardDto);

            var info = _memberCardRepository.Upd(item);

            result.Success(info);

            return result;
        }
    }
}
